/*
 * socketlib.cpp
 *
 *  Created on: 05.03.2012
 *      Author: Caffeine
 */




#include "socketlib.h"

#ifdef WINDOWS_PLATFORM
bool network_liblory_data_loaded=false;
WSADATA network_lib_data;

void loadNetworkLib(){
	if (network_liblory_data_loaded)
		return;

	WSAStartup(0x0202,&network_lib_data);
	network_liblory_data_loaded=true;
}
#endif

SOCK_ADDRESS create_address(){
	SOCK_ADDRESS serv_addr;
	memset((char *)&serv_addr,0,sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	return serv_addr;
}

SOCKET do_accept(SOCKET sock,SOCK_ADDRESS*cl_addr){
#ifdef WINDOWS_PLATFORM
	int clilen;
#endif
#if defined (POSIX_LIBLARY)
    socklen_t clilen;
#endif
	clilen = sizeof(*cl_addr);
	return accept(sock,(struct sockaddr *)cl_addr, &clilen);
}

SOCKET create_server_socket(){
	int reuse = 1;
	SOCKET sd=create_socket();
	if (sd<=0)
		return 0;
	if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
		return 0;
	return sd;
}
